var express = require('express');
var maoyanRouter = express.Router();
var https=require('https');
const cityList = require('../utils/city');

/* 猫眼数据 */
maoyanRouter.get('/', async(req, res, next)=> {
    httpget((data)=>{
        const obj={
            data:JSON.parse(data).data.hot,
            msg:'成功',
            code:'0'
        }
        res.send(obj)
    })
    
});

/* 城市数据 */
maoyanRouter.get('/cityList', async(req, res, next)=> {
    res.send({data:cityList,msg:'成功',code:'0'})
});

function httpget(cb){
    var data="";
    https.get(`https://i.maoyan.com/api/mmdb/movie/v3/list/hot.json?
    ct=%E5%8C%97%E4%BA%AC&ci=1&channelId=4`,(res)=>{
        res.on("data",(chunk)=>{
            data+=chunk
        })
        res.on("end",()=>{
            cb(data)
        })
    })
}

module.exports = maoyanRouter;
